package com.mms.utsc.smartnet;

import java.text.DecimalFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;

public class TimePickerPref extends DialogPreference {

	private int lastHour = 0;
	private int lastMinute = 0;
	private TimePicker picker = null;

	public static int getHour(String time) {
		String[] pieces = time.split(":");

		return (Integer.parseInt(pieces[0]));
	}

	public static int getMinute(String time) {
		String[] pieces = time.split(":");

		return (Integer.parseInt(pieces[1]));
	}

	public TimePickerPref(Context ctxt) {
		this(ctxt, null);
	}

	public TimePickerPref(Context ctxt, AttributeSet attrs) {
		this(ctxt, attrs, 0);
	}

	public TimePickerPref(Context ctxt, AttributeSet attrs, int defStyle) {
		super(ctxt, attrs, defStyle);

		setPositiveButtonText("Set");
		setNegativeButtonText("Cancel");
		String currTime = PreferenceManager.getDefaultSharedPreferences(ctxt).getString("update_time", "00:00");
		setSummary(String.format("%02d", getHour(currTime)) + ":" + String.format("%02d", getMinute(currTime)) + "\nPlease pick a time of the day that you will most likely have Internet connection. Announcements will be checked at this time.");
	}

	@Override
	protected View onCreateDialogView() {
		picker = new TimePicker(getContext());

		return (picker);
	}

	@Override
	protected void onBindDialogView(View v) {
		super.onBindDialogView(v);

		picker.setCurrentHour(lastHour);
		picker.setCurrentMinute(lastMinute);
		resetSummary();
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		if (positiveResult) {
			lastHour = picker.getCurrentHour();
			lastMinute = picker.getCurrentMinute();

			String time = String.valueOf(lastHour) + ":" + String.valueOf(lastMinute);

			if (callChangeListener(time)) {
				persistString(time);
				resetSummary();
				UpdateAnnouncements update = new UpdateAnnouncements();
				
				update.cancelSchedUpdate(this.getContext());
				
				Calendar timeToTrigger = Calendar.getInstance();
				timeToTrigger.set(timeToTrigger.get(Calendar.YEAR), timeToTrigger.get(Calendar.MONTH), timeToTrigger.get(Calendar.DAY_OF_MONTH), getHour(time), getMinute(time));
				update.schedUpdate(this.getContext(), timeToTrigger.getTimeInMillis());
			}
		}
	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return (a.getString(index));
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
		String time = null;

		if (restoreValue) {
			if (defaultValue == null) {
				time = getPersistedString("00:00");
			} else {
				time = getPersistedString(defaultValue.toString());
			}
		} else {
			time = defaultValue.toString();
		}

		lastHour = getHour(time);
		lastMinute = getMinute(time);
	}

	void resetSummary() {
		setSummary(String.format("%02d", lastHour) + ":" + String.format("%02d", lastMinute)
				+ "\nPlease pick a time of the day that you will most likely have Internet connection. Announcements will be checked at this time.");
	}
}
